﻿// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Oliver Rau" email="mail@olira.de"/>
//     <version>$Revision: 3 $</version>
//     <created>12.08.2006 15:15</created>
// </file>

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace ns2kml
{
	/// <summary>
	/// Description of OuiHelper.
	/// </summary>
	public class OuiHelper
	{
		static OuiHelper instance;
		
		public static OuiHelper Instance {
			get {
				if (instance == null)
					instance = new OuiHelper();
				
				return instance;
			}
		}
		
		Dictionary<string, string> ouis;
		
		OuiHelper()
		{
			ouis = new Dictionary<string, string>();
			Assembly exe = typeof(OuiHelper).Assembly;
			string fileName = Path.Combine(Path.GetDirectoryName(exe.Location), "oui.txt");
			
			if (!File.Exists(fileName))
				throw new ApplicationException("Can't find oui file!");
			
			StreamReader sr = new StreamReader(fileName);
			
			string line;
			string key;
			
			while ((line = sr.ReadLine()) != null) {
				if (line != "") {
					key = line.Substring(0, 8).ToLower();
					if (ouis.ContainsKey(key))
						continue;
					
					ouis.Add(key, line.Substring(9));
				}
			}
			
			sr.Close();
		}
		
		public string GetOui(string key)
		{
			if (ouis.ContainsKey(key))
				return ouis[key];
			else
				return "[unknown]";
		}
		
		public Dictionary<string, string> Ouis {
			get {
				return ouis;
			}
		}
		
	}
}
